//
//  DBSearchParkHistory.m
//  SXH
//
//  Created by Jack on 15/9/7.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "DBSearchParkHistory.h"
#import "DBManager.h"

static FMDatabase *shareDataBase = nil;

@implementation DBSearchParkHistory

+ (FMDatabase *)createDataBase {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareDataBase = [FMDatabase databaseWithPath:dataBasePath];
    });
    return shareDataBase;
}

/**
 判断数据库中表是否存在
 **/
+ (BOOL) isTableExist:(NSString *)tableName
{
    FMResultSet *rs = [shareDataBase executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
    while ([rs next])
    {
        // just print out what we've got in a number of formats.
        NSInteger count = [rs intForColumn:@"count"];
        NSLog(@"%@ isOK %ld", tableName,(long)count);
        
        if (0 == count)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    
    return NO;
}

/**
 创建表
 **/
+ (BOOL)createTable {
    debugMethod();
    NSLog(@"%@",dataBasePath);
    if (1){
        {
            shareDataBase = [DBManager createDataBase];
            if ([shareDataBase open]) {
                if (![DBManager isTableExist:@"search_park"]) {
                    NSString *sql = @"create table search_park (name text)";
                    NSLog(@"no search_park ");
                    [shareDataBase executeUpdate:sql];
                }
                [shareDataBase close];
            }
        }
    }
    return YES;
}

/**
 关闭数据库
 **/
+ (void)closeDataBase {
    if(![shareDataBase close]) {
        NSLog(@"数据库关闭异常，请检查");
        return;
    }
}

/**
 删除数据库
 **/
+ (void)deleteDataBase {
    if (shareDataBase != nil) {
        //这里进行数据库表的删除工作
        BOOL isOk = NO;
        shareDataBase = [DBManager createDataBase];
        if ([shareDataBase open]) {
            isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"DELETE FROM \"search_park\" "]];
            [shareDataBase close];
        }
    }
}

+ (BOOL) saveOrUpdataEntity:(NSString*)strRes {
    BOOL isOk = NO;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        NSString *m = nil;
        NSInteger i = 0;
        FMResultSet *s = [shareDataBase executeQuery:@"SELECT * FROM \"search_park\" WHERE (\"name\") = ?",strRes];
        while([s next]) {
            m = [s stringForColumn:@"name"];
            if([m isEqualToString:strRes]){
                i++;
            }
        }
        
        if(i > 0){
            isOk = [shareDataBase executeUpdate:
                    @"UPDATE \"search_park\" set (\"name\") = ? WHERE (\"name\") = ?",strRes,strRes];
        }else{
            isOk = [shareDataBase executeUpdate:@"INSERT INTO \"search_park\" (\"name\") VALUES(?)",strRes];
        }
        [shareDataBase close];
    }
    return isOk;
}

+ (NSMutableArray *) selectAllSearchRes {
    NSMutableArray *mutableArrayRes = [[NSMutableArray alloc] init];
    
    NSString *m = nil;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM \"search_park\""]];
        while([s next]) {
            m = [s stringForColumn:@"name"];
            
            [mutableArrayRes addObject:m];
        }
        [shareDataBase close];
    }
    return mutableArrayRes;
}

@end
